package com.sdtv.basemodule.base;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;

import com.billy.android.loading.Gloading;
import com.blankj.utilcode.util.LogUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.sdtv.basemodule.R;
import com.sdtv.basemodule.mvp.IView;
import com.sdtv.basemodule.mvp.presenter.PresenterDispatch;
import com.sdtv.basemodule.mvp.presenter.PresenterProviders;
import com.sdtv.basemodule.router.ARouterUtils;
import com.sdtv.basemodule.utils.NetworkUtils;
import com.sdtv.basemodule.widget.gloading.StatusConstant;
import com.trello.lifecycle2.android.lifecycle.AndroidLifecycle;
import com.trello.rxlifecycle3.LifecycleProvider;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.basemodule.base
 * @ClassName: BaseActivity
 * @Description: java类作用描述
 * @Author: 小郭同学
 * @CreateDate: 2019-06-19 14:06
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public abstract class BaseActivity extends AppCompatActivity implements IView {

    //butterknife绑定
    private Unbinder unbinder;

    //显示状态view
    private Gloading.Holder holder;
    //presenter注解相关
    protected PresenterProviders mPresenterProviders;
    protected PresenterDispatch mPresenterDispatch;

    /**
     * 软键盘管理
     */
    private InputMethodManager mInputMethodManager;
    //显示dialog
    private Dialog dialog;


    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (0 != getLayoutId()) {
            setContentView(getLayoutId());
            unbinder = ButterKnife.bind(this);
        }
        //初始化presenter
        mPresenterProviders = PresenterProviders.inject(this);
        mPresenterDispatch = new PresenterDispatch(mPresenterProviders);
        //绑定view
        mPresenterDispatch.attachView(this, this);
        mPresenterDispatch.onCreatePresenter(savedInstanceState);
        //Arouter注入
        ARouterUtils.injectActivity(this);
        initStatusBar();
        initView();
        initData();
    }


    /**
     * 初始化状态栏
     */
    protected void initStatusBar() {
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarColor(R.color.colorPrimary)
                //解决软键盘与底部输入框冲突问题
                .keyboardEnable(true)
                .init();
    }


    /**
     * 设置布局文件ID
     *
     * @param
     * @return
     */
    public int getLayoutId() {
        return 0;
    }


    /**
     * 初始化页面布局
     */
    public abstract void initView();

    /**
     * 初始化页面数据
     */
    public abstract void initData();


    @Override
    protected void onDestroy() {
        if (null != unbinder && unbinder != Unbinder.EMPTY) {
            unbinder.unbind();
        }
        mPresenterDispatch.onDestroyPresenter();
        //关闭键盘
        hideSoftKeyBoard();
        super.onDestroy();
    }

    /**
     * 初始化holder
     */
    @Override
    public void initGloadingView() {
        if (null == holder) {
            holder = Gloading.getDefault().wrap(this).
                    withRetry(() -> {
                        onLoadRetry();
                    });
        }
    }

    /**
     * 展示加载框
     */
    @Override
    public void showLoading() {
        if (null == holder) {
            initGloadingView();
        }
        holder.showLoading();
    }

    /**
     * 隐藏加载框
     */
    @Override
    public void hideLoading() {
        if (null == holder) {
            initGloadingView();
        }
        holder.showLoadSuccess();
    }

    /**
     * 显示错误页面
     * --有两种 1、加载数据失败  2. 网络异常错误
     */
    @Override
    public void onError() {
        if (null == holder) {
            initGloadingView();
        }
        if (NetworkUtils.isConnected()) {
            // 网络连接正常--
            holder.showLoadFailed();
        } else {
            holder.showLoadingStatus(StatusConstant.STATUS_NETWORK_ERROR);
        }

    }

    /**
     * 弹出提示信息
     */
    @Override
    public void showMessage() {

    }

    /**
     * 显示空View
     */
    @Override
    public void showEmptyView() {

    }

    /**
     * 重新加载 --子类里面重写
     */
    @Override
    public void onLoadRetry() {

    }

    /**
     * 现在加载中提示框 DIALOG类型
     */
    @Override
    public void showLoadingDialog() {

    }

    /**
     * 隐藏加载中提示框
     */
    @Override
    public void hideLoadingDialog() {

    }

    @Override
    public LifecycleProvider<Lifecycle.Event> getProvider() {
        return AndroidLifecycle.createLifecycleProvider(this);
    }

    /**
     * 隐藏软键盘
     */
    public void hideSoftKeyBoard() {
        View localView = getCurrentFocus();
        if (this.mInputMethodManager == null) {
            this.mInputMethodManager = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
        }
        if ((localView != null) && (this.mInputMethodManager != null)) {
            LogUtils.e("隐藏软键盘呀");
            this.mInputMethodManager.hideSoftInputFromWindow(localView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
